package com.lx.screenshot.floatWindow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.lx.screenshot.databinding.ResultWindowBinding;
import com.lx.screenshot.util.ClipboardUtil;


/**
 * 截屏并显示结果的悬浮窗，主要功能有
 * 1、接收截屏范围
 * 2、根据截屏范围截屏
 * 3、保存截图
 * 4、上传截屏到api识别并翻译
 * 5、显示传回的结果
 * 特点：
 * 1、该悬浮窗可以被拖动
 * 2、点击悬浮窗可以复制结果
 * 3、右上角添加一个关闭按钮
 */
public class ResultWindow extends FloatWindow {
    private float startX;
    private float startY;
    private float endX;
    private float endY;
    private ResultWindowBinding binding;
    private static OnPostFailure onPostFailure;
    private  Bitmap bitmap;

    public ResultWindow(Context context, float startX, float startY, float endX, float endY) {
        super(context);
        this.startX = startX;
        this.startY = startY;
        this.endX = endX;
        this.endY = endY;
    }
    public ResultWindow(Context context, Bitmap bitmap) {
        super(context);
        this.bitmap = bitmap;
    }
    public static void setOnPostFailure(OnPostFailure onPostFailure) {
        ResultWindow.onPostFailure = onPostFailure;
    }

    @Override
    protected void initView() {
        floatWindow = new FrameLayout(context);
        binding = ResultWindowBinding.inflate(LayoutInflater.from(context), floatWindow, false);
        // 截屏并上传
        screenAndPull();
        floatWindow.setBackgroundColor(Color.parseColor("#80000000")); // 设置蒙板的颜色和透明度

        floatWindow.addView(
                binding.getRoot()
        );
    }

    private void screenAndPull() {
        binding.resultTV.setText("正在截屏。。。");
//        ScreenUtil.save(
//                ScreenUtil.screen(startX, startY, endX, endY)
//        );
        //第一次会自动申请录屏权限
//        ScreenRecordUtil.getInstance().screenShot(activity, new OnScreenShotListener() {
//            @Override
//            public void screenShot() {
//                //可以获取截图，可以多次调用
//                Bitmap bitmap = ScreenRecordUtil.getInstance().getScreenShot();
//
//                 Bitmap bitmap =  ScreenUtil.screen(startX, startY, endX, endY);
                System.out.println("图片保存成功");
                binding.ivImage.setImageBitmap(bitmap);
//                binding.resultTV.setText("正在翻译。。。");
//
//                //最后关闭录屏服务
//                ScreenRecordUtil.getInstance().destroy();
//            }
//        });


//        sendToApi();
    }


    @Override
    protected void initParams() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
//        params.format = PixelFormat.RGB_888;

        params.gravity = Gravity.CENTER;

        // 设置浮窗以外的触摸事件可以传递给后面的窗口、不自动获取焦点
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(metrics);
        int w = metrics.widthPixels;
        int h = metrics.heightPixels;
        params.width = w;
        params.height = h;
    }

    @Override
    protected void addClickListener() {
        binding.closeBT.setOnClickListener(v -> {
            destroy();
        });

        floatWindow.setOnClickListener(v->{
            ClipboardUtil.add(context, binding.resultTV.getText().toString());
            Toast.makeText(context, "已复制结果到剪切板", Toast.LENGTH_SHORT).show();
        });
    }
}
